# encoding:utf-8
from __future__ import print_function
from winreg import *
import ctypes, sys

subDir = r'SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName'
actived = r'\ActiveComputerName'
nowname = r'\ComputerName'
enableKey = 'ComputerName'
value = 1

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

def updateFireWall(keyName):
    # 1.连接注册表根键
    regRoot = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
    # 2.获取指定目录下键的控制
    keyHandel = OpenKey(regRoot, r'SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName')
    print(keyHandel)
    if is_admin():
        # 3.设置该键的指定键值enableKey为value
        SetValueEx(keyHandel, enableKey, value, REG_SZ, "test")
        print(keyHandel)
    else:
        if sys.version_info[0] == 3:
            ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
    # 4.关闭键的控制
    CloseKey(keyHandel)
    CloseKey(regRoot)
